package com.example.demo.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.controller.dto.RequestParam;
import com.example.demo.controller.dto.ResponseDto;
import com.example.demo.dao.VaaMapper;
import com.example.demo.entity.Vaa;

@RestController
@RequestMapping("/vaa")
public class IndexController {
    @Resource
    private VaaMapper vaaMapper;
    @GetMapping("/test")
    public String test(){
        return "hello";
    }

    @PostMapping("/list")
    public ResponseDto<?> list(@RequestBody RequestParam param){
        if(param.pageNum==null || param.pageSize==null){
            return new ResponseDto<>(false,"缺少pageNum或者pageSize字",null);
        };
        Page<Vaa> page = new Page<>(param.pageNum,param.pageSize);
        QueryWrapper<Vaa> queryWrapper =new QueryWrapper<>();
        if(param.keyword!=null){
            queryWrapper.like("identificationn", param.keyword);
        };
        return new ResponseDto<>(true,null,vaaMapper.selectPage(page, queryWrapper));

        
    }
    @PostMapping("/add")
    public ResponseDto<?> add(@RequestBody Vaa vaa){
        if(vaa.getIdentificationn()==null) return new ResponseDto<>(false,"标识不能为空",null);
        if(vaa.getIdentificationn()==null) return new ResponseDto<>(false,"标识不能为空",null);
        vaaMapper.insert(vaa);
        return new ResponseDto<>(true,null,"添加成功");
    }

    @GetMapping("/delete")
    public ResponseDto<?> delete(@org.springframework.web.bind.annotation.RequestParam("identificationn") String identificationn)
    {
        if(identificationn==null) return new ResponseDto<>(false,"标识不存在",null);
        vaaMapper.deleteById(identificationn);
        return new ResponseDto<>(true,null,"删除成功");
    }
    @PostMapping("/modify")
    public ResponseDto<?> modify(@RequestBody Vaa vaa){
        if(vaa.getIdentificationn()==null) return new ResponseDto<>(false,"标识不能为空",null);
        vaaMapper.updateById(vaa);
        return new ResponseDto<>(true,null,"修改成功");
    }
}
